package com.soft.sitegenerator.business.hibernate;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import com.soft.sitegenerator.dao.HibernateDAO;
import com.soft.sitegenerator.exceptions.ObjectNotFoundException;

public class Role {
	private Long id;

	private String name;

	private Set profiles = new HashSet();

	private HibernateDAO hibernateDAOHib;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set getProfiles() {
		return profiles;
	}

	public void setProfiles(Set profiles) {
		this.profiles = profiles;
	}

	public void copyRole(Role r) {
		r.setId(id);
		r.setName(name);
	}

	// HIBERNATE DAO
	public HibernateDAO getHibernateDAOHib() {
		return hibernateDAOHib;
	}

	public void setHibernateDAOHib(HibernateDAO hibernateDAOHib) {
		this.hibernateDAOHib = hibernateDAOHib;
	}

	public void load() throws ObjectNotFoundException {
		hibernateDAOHib.load(this, this.id);
	}

	public void save() {
		hibernateDAOHib.save(this);
	}

	public void update() {
		hibernateDAOHib.update(this);
	}

	public void delete() {
		try {
			this.load();
			hibernateDAOHib.delete(this);
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}
	}

	public boolean exist() {
		try {
			hibernateDAOHib.exist(this, this.id);
		} catch (ObjectNotFoundException e) {
			return false;
		}
		return true;
	}

	public Collection findAll() {
		return hibernateDAOHib.find("from Role order by id");
	}

}